using System;
using System.Collections;
using System.Collections.Generic;
using UMC.Data;
using UMC.Web;
using UMC.WorkFlow.Entities;
using UMC.WorkFlow.Form;

namespace UMC.WorkFlow
{


    [Apiumc("WorkFlow", "Staff", Auth = WebAuthType.User, Desc = "流程固定人员")]
    class WorkFlowStaffActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            TaskForm taskForm = null;
            var Task = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Task", k =>
            {
                var Code = this.AsyncDialog("Code", "none");
                if (Code == "none")
                {
                    this.Prompt("缺少参数");
                }
                taskForm = DataFactory.Instance().TaskForm (Code);

                if (taskForm == null)
                {
                    this.Prompt("未有此流程");
                }
                return this.DialogValue(taskForm.TaskId.ToString());

            })).Value;
            if (taskForm == null)
            {
                taskForm =  DataFactory.Instance().TaskForm (Task);

                if (taskForm == null)
                {
                    this.Prompt("未有此流程");
                }
            }
            var DesignKey = this.AsyncDialog("DesignKey", gg =>
            {
                var form = request.SendValues ?? new UMC.Web.WebMeta();
                if (form.ContainsKey("limit") == false)
                {
                    this.Context.Send(new UISectionBuilder(request.Model, request.Command, new WebMeta(request.Arguments.GetDictionary()))
                        .RefreshEvent("WorkFlow.Staff")
                            .Builder(), true);
                }
                var ui = UMC.Web.UISection.Create(new UITitle("流程固定人员"));

                var staffs = new List<TaskStaff>(DataFactory.Instance().TaskStaff(Task));




                foreach (var u in staffs)
                {
                    var text = u.Value;

                    var ui3 = ui.NewSection().AddCell("固定人员", new Web.UIClick(request.Model, request.Command, new WebMeta(request.Arguments.GetDictionary()).Put(gg, u.DesignKey)))
                    .AddCell("参与节点", String.Format("{0}({1})", u.Caption, u.Value));
                }
                if (staffs.Count == 0)
                {
                    ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未设置固定参与人员").Put("icon", "\uEA05")
                        , new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),
                 new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));//.Name 

                }


                response.Redirect(ui);
                return this.DialogValue("none");
            });
            var userId = this.AsyncDialog("UserId", "Settings", "SelectUser");


            DataFactory.Instance().Put(new TaskStaff
            {
                TaskId = taskForm.TaskId,
                DesignKey = DesignKey,
                Value = userId,
                Caption = (request.SendValues ?? request.Arguments)["UserId_Text"]
            });
            this.Context.Send("WorkFlow.Staff", true);


        }
    }
}